﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace BoriPeter.FitnessJournal8.Converters
{
    public class DateToNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            DateTime date = (DateTime)value;
            string dateName = "";
            if (IsDateEqual(date, DateTime.Now))
                dateName = "Today";
            else if (IsDateEqual(date, DateTime.Now.AddDays(-1)))
                dateName = "Yesterday";
            else if (IsDateEqual(date, DateTime.Now.AddDays(1)))
                dateName = "Tommorow";
            else
            {
                TimeSpan SinceEpoch = DateTime.Now - date;
                var totalDays = Math.Round(SinceEpoch.TotalDays) - 1;
                dateName = totalDays.ToString() + " days ago";
            }

            if (parameter != null)
                dateName = date.ToString("yyyy-MM-dd") + " (" + dateName + ")"; 

            return dateName;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }

        private bool IsDateEqual(DateTime _date1, DateTime _date2)
        {
            if ((_date1.Year == _date2.Year) && (_date1.Month == _date2.Month) && (_date1.Day == _date2.Day))
                return true;
            else
                return false;
        }
    }
}
